/*
 * @Author: Jiraiya
 * @Date: 2020-03-16 18:19:24
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-03-16 18:33:36
 * @Description: 
 */
console.log(0.1.toString(2))
// 0.0001100110011001100110011001100110011001100110011001101

// 0.1*2=0.2 0
// 0.2*2=0.4 0
// 0.4*2=0.8 0
// 0.8*2=1.6 1
// 0.6*2=1.2 1
// 0.2*2=0.4 0
// 无线循环

console.log((0.2.toString(2) + '').length - 2) //54
// 0.001100110011001100110011001100110011001100110011001101


/**
 * 精确加法
 */
function add(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length;
    const num2Digits = (num2.toString().split('.')[1] || '').length;
    const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
    return (num1 * baseNum + num2 * baseNum) / baseNum;
}

console.log(add(0.1, 0.2)) //0.3